//807.保持城市天际线
//https://leetcode.cn/problems/max-increase-to-keep-city-skyline
class Solution {
public:
    int maxIncreaseKeepingSkyline(vector<vector<int>>& grid) {
        //记录每一行每一列中最高的
        //其他高度只要不超过最高的就行了
        int m = grid.size(),n = grid[0].size();
        vector<int> maxrow(m),maxcol(n);
        for(int i = 0;i<m;++i)
            for(int k = 0;k<n;++k)
            {
                if(maxrow[i] < grid[i][k]) maxrow[i] = grid[i][k];
                if(maxcol[k] < grid[i][k]) maxcol[k] = grid[i][k];
            }

        int ret = 0;
        for(int i = 0;i<m;++i)
            for(int k = 0;k<n;++k)
                //这里取min行和列最大值中的最小值
                ret += min(maxrow[i],maxcol[k]) - grid[i][k];

        return ret;
    }
};